package com.manger.thread;

import com.manger.format.CaseFilesFormat;
import com.manger.model.Project;
import com.manger.utils.DeCompressUtils;
import com.manger.utils.DetNameEnum;
import com.manger.utils.ProjectUtils;
import com.manger.utils.SystemConstant;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CaseFilesFormatThreadBak extends Thread {

    private String targetDirPath;

    public CaseFilesFormatThreadBak(String targetDirPath) {
        this.targetDirPath = targetDirPath;
    }

    @Override
    public void run() {
        String dir = SystemConstant.getFormatDir();
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
            JOptionPane.showMessageDialog(null, "没有找到需要整理的案件！", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        File[] files = dirFile.listFiles();
        List<File> formatFiles = new ArrayList<File>();
        for (File file : files) {
            if (file.getName().endsWith(".zip")) {
                formatFiles.add(file);
            }
        }
        if (formatFiles.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有找到需要整理的案件！", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        List<String> formatDirPathList = new ArrayList<>();
        long date = System.currentTimeMillis();
        try {
            for (File file : formatFiles) {
                String destDir = SystemConstant.getFormatDir() + File.separator + date + File.separator + file.getName().substring(0, file.getName().lastIndexOf("."));
                File temp = new File(destDir);
                System.out.println(temp.getCanonicalPath());
                if(destDir.contains("桔多多")){

                }
                if(!temp.exists()){
                    temp.mkdirs();
                }
                DeCompressUtils.unZip(file.getAbsolutePath(), destDir);
                formatDirPathList.add(destDir);
                System.out.println(destDir);
                System.out.println("解压成功：【" + file.getCanonicalPath() + "】");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "解压失败，请确认文件是否是标准zip压缩文件！", "警告", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
            return;
        }
        if (formatDirPathList.size() <= 0) {
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String formatResultDir = targetDirPath + File.separator + sdf.format(new Date());
        StringBuffer errorStringBuffer = new StringBuffer();
        for (String path : formatDirPathList) {
            try {
                DetNameEnum detNameEnum = DetNameEnum.getDetNameEnumByEndWith(path);
                if (detNameEnum == null) {
                    System.out.println("没有找到路径，请检查文件夹名称是否正确！【" + path + "】");
                    continue;
                }
                Project project = ProjectUtils.projectMap.get(detNameEnum);
                Class<? extends CaseFilesFormat> clazz = detNameEnum.getClazz();

                Object object = clazz.newInstance();
                System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
                System.out.println("开始整理：【"+project.getDebtName()+"】");
                System.out.println(path);
                ((CaseFilesFormat) object).format(path, formatResultDir, project);
            } catch (Exception e) {
                e.printStackTrace();
                errorStringBuffer.append(path).append("\n").append(e.getMessage()).append("\n");
            }
        }
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("【整理结束】");
        File file = new File(SystemConstant.getTempFileDir());
        File[] fileList = file.listFiles();
        for (File file2 : fileList) {
            file2.delete();
        }
        String data = errorStringBuffer.toString();
        if(StringUtils.isNotEmpty(data)){
            System.out.println("【错误信息】");
            System.out.println(errorStringBuffer.toString());
        }
    }
}
